package com.atguigu.mySmartCampus.Service.impl;

import com.atguigu.mySmartCampus.mapper.TeacherMapper;
import com.atguigu.mySmartCampus.Service.TeacherService;
import com.atguigu.mySmartCampus.entity.LoginForm;
import com.atguigu.mySmartCampus.entity.Teacher;
import com.atguigu.mySmartCampus.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Override
    public Teacher login(LoginForm loginForm) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("name", loginForm.getUsername());
        qw.eq("password", MD5.encrypt(loginForm.getPassword()));
        Teacher teacher = baseMapper.selectOne(qw);
        return teacher;
    }

    @Override
    public Teacher getTeacherById(Long teacherId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("id", teacherId);
        Teacher teacher = baseMapper.selectOne(qw);
        return teacher;
    }

    @Override
    public IPage<Teacher> getTeacherByOperator(Page<Teacher> page, Teacher teacher) {
        //..

//        QueryWrapper qw = new QueryWrapper();
//        qw.eq("name",teacher.getName());
//        qw.eq("password",teacher.getPassword());
//        Page page1 = baseMapper.selectPage(page, qw);
//        return page1;

        QueryWrapper qw = new QueryWrapper();
        if (!StringUtils.isEmpty(teacher.getName())) {qw.like("name", teacher.getName());}
        if (!StringUtils.isEmpty(teacher.getClazzName())){ qw.like("clazz_name", teacher.getClazzName());}

        //+
        qw.orderByDesc("id");

        Page page1 = baseMapper.selectPage(page, qw);
        return page1;
    }
}
